// Problem 17: Number letter counts
// If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
// If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
// NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
// --------
// ref: https://github.com/frrad/project-euler/blob/master/golang/Problem017.go

package main

import (
	"fmt"
)

func spell(n int) string {
	var words = [...]string{
		"",
		"one",
		"two",
		"three",
		"four",
		"five",
		"six",
		"seven",
		"eight",
		"nine",
		"ten",
		"eleven",
		"twelve",
		"thirteen",
		"fourteen",
		"fifteen",
		"sixteen",
		"seventeen",
		"eighteen",
		"nineteen",
	}
	if n < 20 {
		return words[n]
	}
	if n > 19 && n < 100 {
		ones := n % 10
		switch tens := n / 10; tens {
		case 2:
			return "twenty " + spell(ones)
		case 3:
			return "thirty " + spell(ones)
		case 4:
			return "forty " + spell(ones)
		case 5:
			return "fifty " + spell(ones)
		case 6:
			return "sixty " + spell(ones)
		case 7:
			return "seventy " + spell(ones)
		case 8:
			return "eighty " + spell(ones)
		case 9:
			return "ninety " + spell(ones)
		}
	}

	if n > 99 && n < 1000 {
		ending := n % 100
		hundreds := n / 100
		var endSpell string
		if ending == 0 {
			endSpell = ""
		} else {
			endSpell = " and " + spell(ending)
		}
		return spell(hundreds) + " hundred" + endSpell
	}
	if n == 1000 {
		return "one thousand"
	}
	return ""
}

func count(word string) int {
	if word == "" {
		return 0
	}
	if string(word[0]) == " " {
		return count(word[1:])
	}
	return 1 + count(word[1:])
}

func p017() int {
	sum := 0
	for i := 1; i <= 1000; i++ {
		sum += count(spell(i))
	}
	fmt.Println("Problem 017:", sum)
	return sum
}
